package org.barad.voiceuserinterface.widgets;

import org.apache.log4j.Logger;
import org.barad.voiceuserinterface.SpeechProcessingManager;

import static org.barad.voiceuserinterface.VoiceInterfaceProperties.INTERMEDIATE_INTERACTION_PAUSE;
import static org.barad.voiceuserinterface.VoiceInterfaceProperties.DEBUG;

public class Action extends Widget {
	//TODO: This is temporary accept value
	private static final String TRIGGER_ACTION_VALUE = "five";
	private String text;
	private Logger log = Logger.getLogger(getClass());

	@Override
	public void interact() {
		if (DEBUG) {
			log.info("The possible actions is " + text);
		}
		if (enabled) {
			outputText("Possible action");
			pause(INTERMEDIATE_INTERACTION_PAUSE);
			SpeechProcessingManager.getInstance().speak("Answer with " + TRIGGER_ACTION_VALUE + "  to perform this action", wordsPerMinute);
			inputText();
		}
	}
	
	public void inputText() {
		String result = SpeechProcessingManager.getInstance().listen();
		if (!isEmpty(result) && result.equals(TRIGGER_ACTION_VALUE)) {
			if (DEBUG) {
				log.info("Action " + text + " performed");
			}
			//trigger an event
		} 
		if (DEBUG) {
			log.info("Selceted value is: " + result);
		}
	}
	
	public void outputText(String additionalMessage) {
		String outputText = null;
		if (!isEmpty(additionalMessage)) {
			outputText = additionalMessage + " ";
		}
		if (!isEmpty(text)) {
			outputText = outputText + text;
		} 
		SpeechProcessingManager.getInstance().speak(outputText, super.wordsPerMinute);
	}
	
	/**
	 * Pauses the current thread
	 * @param miliseconds The time in miliseconds
	 */
	private void pause(long miliseconds) {
		try {
			Thread.sleep(miliseconds);
		} catch (InterruptedException ie) {
			/*ignore*/
		}
	}
	
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
}
